﻿using Black.Minecraft.Editor.ViewModels.Core;
using Black.NBT;
using System.Linq;

namespace Black.Minecraft.Editor.ViewModels.DataModels
{
    /// <summary>
    /// Template de DataModel
    /// </summary>
    public class EnchantItemDataModel : BaseViewModel
    {
        #region Propriétés

        /// <summary>
        /// Obtient la définition de l'enchantement
        /// </summary>
        public EnchantDefDataModel Definition
        {
            get
            {
                return (this._definition);
            }
            private set
            {
                if ( this._definition != value)
                {
                    this._definition = value;
                    this.OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// Obtient ou définit le nombre de l'élément
        /// </summary>
        public short ID
        {
            get
            {
                return ((this._data["id"] as TagShort).Value);
            }
            set
            {
                if ((this._data["id"] as TagShort).Value != value)
                {
                    (this._data["id"] as TagShort).Value = value;

                    this.Definition = (from p in MainViewModel.Instance.EnchantsDefinitionSource
                                        where p.Id == this.ID
                                        select p).FirstOrDefault();

                    this.OnPropertyChanged();
                    this.OnPropertyChanged("ToolTip");
                    MainViewModel.Instance.IsModified = true;
                }
            }
        }

        /// <summary>
        /// Obtient ou définit le nombre de l'élément
        /// </summary>
        public short Level
        {
            get
            {
                return ((this._data["lvl"] as TagShort).Value);
            }
            set
            {
                if ((this._data["lvl"] as TagShort).Value != value)
                {
                    (this._data["lvl"] as TagShort).Value = value;
                    this.OnPropertyChanged();
                    this.OnPropertyChanged("ToolTip");
                    MainViewModel.Instance.IsModified = true;
                }
            }
        }

        /// <summary>
        /// Obtient le tooltip de l'élément
        /// </summary>
        public string ToolTip
        {
            get
            {
                if (this.Definition != null)
                    return (string.Format("{0} {1}", this.Definition.Label, EnchantItemDataModel.ConvertToGameLevel(this.Level)));

                return (string.Empty);
            }
        }

        #endregion

        #region Variables d'instances

        private TagCompound _data;
        private EnchantDefDataModel _definition;

        #endregion

        #region Constructeurs

        /// <summary>
        /// Constructeu de la classe
        /// </summary>
        /// <param name="id">Identifiant enchantement</param>
        /// <param name="level">Niveau enchantement</param>
        public EnchantItemDataModel(short id, short level)
            : base()
        {
            this._data = new TagCompound("");
            this._data.Items.Add(new TagShort("id", id));
            this._data.Items.Add(new TagShort("lvl", level));

            // Recherche définition

            this._definition = (from p in MainViewModel.Instance.EnchantsDefinitionSource
                                where p.Id == this.ID
                                select p).FirstOrDefault();
        }

        /// <summary>
        /// Constructeu de la classe
        /// </summary>
        /// <param name="data">Source de donnée</param>
        public EnchantItemDataModel(TagCompound data)
            : base()
        {
            this._data = data;

            // Recherche définition

            this._definition = (from p in MainViewModel.Instance.EnchantsDefinitionSource
                                where p.Id == this.ID
                                select p).FirstOrDefault();
        }

        #endregion

        #region Fonctions publiques

        /// <summary>
        /// Retourne la source de donnée
        /// </summary>
        /// <returns></returns>
        public TagCompound GetData()
        {
            return (this._data);
        }

        #endregion

        #region Fonctions statiques

        /// <summary>
        /// Convertie un niveau en chaine de jeu
        /// </summary>
        /// <param name="level">Niveau a traiter</param>
        /// <returns>Chaine en jeu</returns>
        public static string ConvertToGameLevel(short level)
        {
            switch (level)
            {
                case (1):
                    return ("I");

                case (2):
                    return ("II");

                case (3):
                    return ("III");

                case (4):
                    return ("IV");

                case (5):
                    return ("V");

                default:
                    return ("?");
            }
        }

        #endregion
    }
}
